//
//  MSCustomTabbar.swift
//  weibo
//
//  Created by JLHong on 15/12/15.
//  Copyright © 2015年 long. All rights reserved.
//

import UIKit 

class MSCustomTabbar: UITabBar {
    
    private let conut = 5
    
        //MARK:-- 懒加载加号按钮  --
    lazy  var tabbarBtn : UIButton = {
        let btn = UIButton()
        
        //添加背景图片
        btn .setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //添加背景图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加监听
        btn.addTarget(self, action: "tabbarBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //MARK:  -
        return btn
    }()
    
    //按钮监听方法
     func tabbarBtnClick () {
        print("点击了")
    }
    
       //MARK:- -  布局子空间  - -
    override func layoutSubviews() {
        super.layoutSubviews()
        self.addSubview(tabbarBtn)
        
        let width = bounds.width / CGFloat(conut)
        let frame  = CGRect(x: 0, y: 0, width: width, height: bounds.height)
        
        var index = 0
        for view in subviews {
            if (view is UIControl) && !(view is UIButton) {
                
                view.frame = CGRectOffset(frame, CGFloat( index) * width, 0)
                
                index += index == 1 ? 2 : 1
            }
            tabbarBtn.frame = CGRectOffset(frame, 2 * width, 0)
        }
    }
}
